package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/roles")
@Slf4j
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    //查询角色列表
    @GetMapping
    public Result roleList(String name, String label,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize){
    log.info("查询角色列表:{},{},{},{}",name,label,page,pageSize);
        PageResult<Role> pageResult=roleService.roleList(name, label, page, pageSize);
        return Result.success(pageResult);
    }
    //根据ID删除角色
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除角色:{}",id);
        roleService.delete(id);
        return Result.success();
    }
    //添加角色
    @Log
    @PostMapping
    public Result add(@RequestBody Role role){
        log.info("添加角色:{}",role);
        roleService.add(role);
        return Result.success();
    }
    //根据ID查询角色
    @GetMapping("/{id}")
    public Result getByID(@PathVariable Integer id) {
        log.info("根据ID查询角色"+id);
        Role role = roleService.getByID(id);
        return Result.success(role);
    }
    //更新角色
    @Log
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("更新角色:{}",role);
        roleService.upDate(role);
        return Result.success();
    }
    //查询所有角色
    @GetMapping("/list")
    public Result allList(Role role){
        log.info("查询所有角色:{}",role);
        return roleService.allList( role);
    }
}
